package com.df.bpm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.df.bpm.dto.InformatizationOutputDTO;
import com.df.common.service.impl.BaseServiceImpl;
import com.df.bpm.entity.InformatizationOutput;
import com.df.bpm.mapper.InformatizationOutputMapper;
import com.df.bpm.service.InformatizationOutputService;

import com.df.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 需求预期输出服务
 */
@Service
@RequiredArgsConstructor
public class InformatizationOutputServiceImpl extends BaseServiceImpl<InformatizationOutputMapper,InformatizationOutput> implements InformatizationOutputService {
	private final InformatizationOutputMapper informatizationOutputMapper;

    public void saveOutputs4RequirementId(Integer requirementId, List<InformatizationOutputDTO> outputs) {
        StringBuilder outputsId= null;
        for(InformatizationOutputDTO output:outputs){
            Integer index=output.getInformatizationOutputIndex();
            if(CommonUtils.varIsNotBlank(index)){
                if(outputsId==null){
                    outputsId = new StringBuilder("" + index);
                }else{
                    outputsId.append(",").append(index);
                }
            }
        }
        if(CommonUtils.varIsNotBlank(outputsId)){
            informatizationOutputMapper.delete4Save(requirementId,outputsId.toString());
        }else{
            informatizationOutputMapper.delete(new QueryWrapper<InformatizationOutput>().eq("informatizationRequirementIndex",requirementId));
        }
        for(InformatizationOutputDTO output:outputs){
            if(CommonUtils.varIsNotBlank(output.getInformatizationOutputIndex())){
                if(CommonUtils.varIsNotBlank(output.getBusinessEntityIndex()) &&
                        -1!=(output.getBusinessEntityIndex())) {
                    informatizationOutputMapper.updateById(output);
                }else{
                    informatizationOutputMapper.deleteById(output);
                }
            }else{
                if(CommonUtils.varIsNotBlank(output.getBusinessEntityIndex())){
                    output.setInformatizationRequirementIndex(requirementId);
                    informatizationOutputMapper.insert(output);
                }
            }
        }
    }
}